using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace ToDoListWeb.Models
{
    public partial class ToDoListWebContext : DbContext
    {
        public virtual DbSet<Inventory> Inventory { get; set; }
        public virtual DbSet<Matter> Matter { get; set; }
        public virtual DbSet<Type> Type { get; set; }
        public virtual DbSet<User> Users { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
            optionsBuilder.UseSqlServer(@"Data Source =.; Initial Catalog = ToDoListWeb; User ID = sa; Password =W238498;MultipleActiveResultSets=true;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Inventory>(entity =>
            {
                entity.Property(e => e.InventoryName)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.HasOne(d => d.Type)
                    .WithMany(p => p.Inventory)
                    .HasForeignKey(d => d.TypeId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Inventory_Type");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Inventory)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Inventory_Users");
            });

            modelBuilder.Entity<Matter>(entity =>
            {
                entity.Property(e => e.CreatTime).HasColumnType("date");

                entity.Property(e => e.MatterContent).IsRequired();

                entity.Property(e => e.OverTime).HasColumnType("date");

                entity.Property(e => e.Remark).HasMaxLength(50);

                entity.HasOne(d => d.Inventory)
                    .WithMany(p => p.Matter)
                    .HasForeignKey(d => d.InventoryId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Matter_Inventory");

                entity.HasOne(d => d.Type)
                    .WithMany(p => p.Matter)
                    .HasForeignKey(d => d.TypeId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Matter_Type");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Matter)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Matter_Users");
            });

            modelBuilder.Entity<Type>(entity =>
            {
                entity.Property(e => e.TypeName)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Type)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Type_Users");
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.UserId)
                    .HasName("PK_Users");

                entity.Property(e => e.NickName).HasMaxLength(50);

                entity.Property(e => e.Password)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.RegistTime).HasColumnType("date");

                entity.Property(e => e.UserName)
                    .IsRequired()
                    .HasMaxLength(50);
            });
        }
    }
}